package com.shadt.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shadt.core.model.Json;
import com.shadt.core.model.Tree;
import com.shadt.core.service.ResourceService;
import com.shadt.core.util.ConfigUtil;
import com.shadt.core.util.SessionInfo;
import com.shadt.core.vo.ResourceVo;

/**
 * 功能栏目控制器
 * 
 * @author SongR
 * 
 */
@Controller
@RequestMapping("/resourceAction")
public class ResourceController {
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(ResourceController.class);

	@Autowired
	ResourceService resourceService;

	/**
	 * 根据用户信息获取用户可以访问的栏目信息
	 * 
	 * @param sessionInfo
	 * @return
	 */
	@RequestMapping("/menu")
	@ResponseBody
	public Json getByUser(HttpSession session) {
		Json json = new Json();
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil
				.getSessionInfoName());
		List<Tree> tList = resourceService.getByUser(sessionInfo);
		json.setObj(tList);
		return json;
	}

	/**
	 * 跳转到资源管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/toResourcePage")
	public String toResourcePage() {
		return "page/resourcemanager";
	}

	/**
	 * 添加资源
	 * 
	 * @return
	 */
	@RequestMapping("/addResource")
	@ResponseBody
	public Json addResource(ResourceVo resourceVo) {
		Json j = new Json();
		try {
			resourceService.addResource(resourceVo);
			j.setMsg("添加成功！");
			j.setSuccess(true);
			j.setObj(resourceVo);
		} catch (Exception e) {
			j.setMsg("添加失败！请联系管理员！");
			e.printStackTrace();
		}
		// System.out.println("addResource-->");
		return j;
	}

	/**
	 * 获取所有资源
	 * 
	 * @return
	 */
	@RequestMapping("/getAllResource")
	@ResponseBody
	public String getAllResource(HttpServletRequest request, String name,
			String resourceTypeId) {
		String names = request.getParameter("aoData");
		JSONArray jsonarray = JSONArray.parseArray(names);
		String sEcho = "", iDisplayStart = "", iDisplayLength = "", sSearch = "";
		for (int i = 0; i < jsonarray.size(); i++) {
			JSONObject obj = (JSONObject) jsonarray.get(i);
			if (obj.get("name").equals("sEcho"))
				sEcho = obj.get("value").toString();
			if (obj.get("name").equals("iDisplayStart")) // 开始显示行
				iDisplayStart = obj.get("value").toString();
			if (obj.get("name").equals("iDisplayLength")) // 每页显示的长度
				iDisplayLength = obj.get("value").toString();
			if (obj.get("name").equals("sSearch"))
				sSearch = obj.get("value").toString();
		}
		List<ResourceVo> resList = resourceService.getAllResource(sSearch,
				Integer.parseInt(iDisplayStart),
				Integer.parseInt(iDisplayLength), resourceTypeId);
		String aaData = JSONArray.toJSONString(resList).toString();
		String json = "{\"sEcho\":" + sEcho + ",\"iTotalRecords\":"
				+ resourceService.getResourceCount(resourceTypeId, sSearch)
				+ ",\"iTotalDisplayRecords\":"
				+ resourceService.getResourceCount(resourceTypeId, sSearch)
				+ ",\"aaData\":" + aaData + "}";
		return json;
	}

	/**
	 * 删除资源信息
	 * 
	 * @return
	 */
	@RequestMapping("/delResource")
	@ResponseBody
	public Json delResource(String id) {
		Json j = new Json();
		try {
			resourceService.delResource(id);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系统出错！请联系管理员！");
		}
		return j;
	}

	/**
	 * 跳转到栏目菜单管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/toMenuManager")
	public String toMenuManager() {
		return "page/menumanager";
	}

	/**
	 * 获取所有资源信息加载到授权列表
	 * 
	 * @return
	 */
	@RequestMapping("/getAllResourceToEmpower")
	@ResponseBody
	public Json getAllResourceToEmpower(HttpServletRequest request,
			String resourceTypeId, String id) {
		Json j = new Json();
		List<ResourceVo> resourceVoList = resourceService
				.getAllResourceToEmpower(resourceTypeId, id);
		j.setObj(resourceVoList);
		j.setSuccess(true);
		return j;
	}

	/**
	 * 资源授权
	 * 
	 * @return
	 */
	@RequestMapping("/addEmpower")
	@ResponseBody
	public Json addEmpower(HttpServletRequest request, HttpSession session,
			String resourceIds, String roleId, String resourceTypeId) {
//		System.out.println("addEmpower");
		System.out.println("resourceIds = " + resourceIds + "===roleId==="
				+ roleId + "===resourceTypeId===" + resourceTypeId);
		Json j = new Json();
		try {
			resourceService.addResourceToEmpower(resourceIds,roleId);
			j.setSuccess(true);
			j.setMsg("授权成功！");
		} catch (Exception e) {
			j.setMsg("授权失败！请联系管理员！");
			e.printStackTrace();
		}
		return j;
	}

}
